<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:yuyanshou'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起预验收') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectname }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)">
          <span style="text-decoration: underline; margin-right: 5px">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
        
      </template>
      <!-- 勾选 -->
      <template #checkBox="{ record, column }">
        <!-- {{ [column.dataIndex[0]] }}
        {{ [column.dataIndex[1]] }} -->
        <a-checkbox
          :checked="record[column.dataIndex[0]][column.dataIndex[1]] == '1' ? true : false"
        />
      </template>
      <template #uploadFile="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          readonly
          :bizKey="record.id"
          :bizType="'PmPreAcceptance_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
      <template #caozuo="{ record }">
        <a @click="handCheck({ id: record.id })" v-if="record.bneedMatCheck" class="margin-r-10">
          <!-- <Icon icon="ant-design:check-circle-outlined" size="20" /> -->
          材料核对
        </a>
        <!-- <a @click="handlePush({ id: record.id })" v-if="record.bpush">
          竣工准备就绪
        </a> -->
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAcceptPrePmPreacceptanceList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmPreacceptanceDelete,
    pmPreacceptanceListData,
    pmPreacceptanceCheck,
    pmPreacceptancePush,
  } from '/@/api/accept/pre/pmPreacceptance';
  import { pmPreacceptanceDisable, pmPreacceptanceEnable } from '/@/api/accept/pre/pmPreacceptance';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { BasicUpload } from '/@/components/Upload';
  import InputForm from './form.vue';
  import { useUserStore } from '/@/store/modules/user';

  const userStore = useUserStore();

  const { t } = useI18n('accept.pre.pmPreacceptance');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('预验收管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工程编号'),
        field: 'viewCode',
        component: 'Input',
      },
      {
        label: t('工程名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('验收方式'),
        field: 'canfilecheck',
        component: 'Select',
        componentProps: {
          dictType: 'zf_ys_type',
        },
      },
      // {
      //   label: t('状态'),
      //   field: 'status',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_pre_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('预验收编号'),
    //   dataIndex: 'djno',
    //   key: 'a.preacceptanceno',
    //   sorter: true,
    //   width: 150,
    //   align: 'left',
    //   fixed: 'left',
    //   slot: 'firstColumn',
    // },
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'projectno',
      fixed: 'left',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.name',
      sorter: true,
      width: 280,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('发起时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 120,
      align: 'left',
      
    },
    {
      title: t('发起人'),
      dataIndex: 'createByName',
      key: 'a.sponsor',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('验收方式'),
      dataIndex: 'canfilecheck',
      key: 'a.canfilecheck',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'zf_ys_type',
    },
    {
      title: t('预验收结果'),
      dataIndex: 'checkresult',
      key: 'a.checkresult',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: '',
    },
    {
      title: t('负责人名称'),
      dataIndex: 'usernamepre',
      key: 'a.usernamepre',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('整改完成'),
      dataIndex: 'acceptCheck.zgStatus',
      key: 'accept.zg_status',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('废管处理'),
      dataIndex: 'acceptCheck.fgStatus',
      key: 'accept.fg_status',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否需中间验收'),
      dataIndex: 'acceptCheck.bneedMid',
      key: 'accept.bneedMid',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('中间验收(条件)'),
      dataIndex: 'acceptCheck.zjysStatus',
      key: 'accept.zjys_status',
      sorter: true,
      width: 120,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否通水推送'),
      dataIndex: 'acceptCheck.bpushGw',
      key: 'accept.bpushGw',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('材料核对'),
      dataIndex: 'acceptCheck.bmatCheck',
      key: 'accept.bmatcheck',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否满足竣工条件'),
      dataIndex: 'acceptCheck.canover',
      key: 'accept.canover',
      sorter: false,
      width: 150,
      dictType: 'zf_jg_canOver',
      align: 'center',
      // fixed: 'right',
    },
    {
      title: t('材料核对'),
      dataIndex: 'checkmaterial',
      key: 'a.checkmaterial',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('材料核对时间'),
      dataIndex: 'checkmaterialtime',
      key: 'a.checkmaterialtime',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('验收图片'),
      dataIndex: 'upload',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'uploadFile',
    },
    // {
    //   title: t('是否推送管网'),
    //   dataIndex: 'pushgw',
    //   sorter: true,
    //   width: 120,
    //   align: 'center',
    //   dictType: 'zf_isPush_gw_pushgw',
    // },
    // {
    //   title: t('状态'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'zf_pre_status',
    //   fixed: 'right',
    // },
    // {
    //   title: t('操作'),
    //   sorter: true,
    //   width: 120,
    //   align: 'center',
    //   slot: 'caozuo',
    //   fixed: 'right',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 130,
    align: 'left',
    actions: (record: Recordable) => [
      // {
      //   icon: 'clarity:note-edit-line',
      //   title: t('编辑预验收'),
      //   onClick: handleForm.bind(this, { id: record.id }),
      //   auth: 'btn:edit:yuyanshou',
      //   ifShow: record.status != '0',
      // },
      // {
      //   icon: 'ant-design:stop-outlined',
      //   color: 'error',
      //   title: t('停用预验收'),
      //   popConfirm: {
      //     title: t('是否确认停用预验收'),
      //     confirm: handleDisable.bind(this, { id: record.id }),
      //   },
      //   auth: 'accept:pre:pmPreacceptance:edit',
      //   ifShow: () => record.status === '0',
      // },
      // {
      //   icon: 'ant-design:check-circle-outlined',
      //   color: 'success',
      //   title: t('启用预验收'),
      //   popConfirm: {
      //     title: t('是否确认启用预验收'),
      //     confirm: handleEnable.bind(this, { id: record.id }),
      //   },
      //   auth: 'accept:pre:pmPreacceptance:edit',
      //   ifShow: () => record.status === '2',
      // },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除预验收'),
        popConfirm: {
          title: t('是否确认删除预验收'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'accept:pre:pmPreacceptance:delete',
      },
      {
        // icon: 'ant-design:file-done-outlined',
        // title: t('材料核对'),
        label: t('材料核对'),
        onClick: handCheck.bind(this, { id: record.id }),
        auth: 'btn:cailiaohedui',
        ifShow: () => record.bneedMatCheck,
      },
      // {
      //   icon: 'ant-design:check-outlined',
      //   title: t('竣工准备就绪'),
      //   onClick: handlePush.bind(this, { id: record.id }),
      //   auth: 'btn:jungongzhunbei',
      //   ifShow: () => record.bpush,
      // },
      // {
      //   icon: 'ant-design:send-outlined',
      //   title: t('发起竣工'),
      //   onClick: handleInitiate.bind(this, { id: record.id }),
      //   auth: 'btn:add:jungong',
      // },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] = useTable({
    api: pmPreacceptanceListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await pmPreacceptanceDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await pmPreacceptanceEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await pmPreacceptanceDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 材料核对
  async function handCheck(record: Recordable) {
    const res = await pmPreacceptanceCheck(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 竣工准备就绪
  async function handlePush(record: Recordable) {
    const res = await pmPreacceptancePush(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 发起竣工
  async function handleInitiate(record: Recordable) {
    console.log(record, '发起竣工');
  }

  // 默认查询条件
  onMounted(async () => {
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      viewCode: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      viewCode: userStore.getProjecte.code,
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
<style scoped>
  .margin-r-10 {
    margin-right: 10px;
  }
</style>
